//
//  PzHomeCtrl.swift
//  Pz
//
//  Created by 蓝鳍互娱 on 2024/6/26.
//

import Foundation
import UIKit
import iAd
class PzHomeCtrl: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = true
        view.backgroundColor = .clear
        creatSubView()
        PzGuideHelper.shareManage.showGuideAlert()
        PzGuideHelper.shareManage.guideFinishBlock = {
            self.makeVipStatus {
                
            }
        }
    }
    lazy var homeBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzHomeBg")
        return imageView
    }()
    lazy var homeSetBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeSet), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeSetIcon"), for: .normal)
        return btn
    }()
    lazy var homeHotTitleImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzHomeHotTitleIcon")
        return imageView
    }()
    lazy var homeHeadHImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzHomeHeadHIcon")
        return imageView
    }()
    lazy var homeHeadFImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pzHomeHeadFIcon")
        return imageView
    }()
    lazy var homeHotGoBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeHot), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeHotGoIcon"), for: .normal)
        return btn
    }()
    lazy var homeOneChipBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeOneChip), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeOneChipIcon"), for: .normal)
        return btn
    }()
    lazy var homeTwoChipBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeTwoChip), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeTwoChipIcon"), for: .normal)
        return btn
    }()
    lazy var homeNorChipBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeNorChip), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeNorIcon"), for: .normal)
        return btn
    }()
    lazy var homeCrachCipeBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeCrachCipe), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeCionIcon"), for: .normal)
        return btn
    }()
    lazy var homeCrachCipeBtn: av = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(homeCrachCipe), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "pzHomeCionIcon"), for: .normal)
        return btn
    }()
    func creatSubView(){
        self.view.addSubview(homeBgImgView)
        self.view.addSubview(homeSetBtn)
        self.view.addSubview(homeHotTitleImgView)
        self.view.addSubview(homeHotGoBtn)
        self.view.addSubview(homeHeadHImgView)
        self.view.addSubview(homeHeadFImgView)
        self.view.addSubview(homeOneChipBtn)
        self.view.addSubview(homeTwoChipBtn)
        self.view.addSubview(homeNorChipBtn)
        self.view.addSubview(homeCrachCipeBtn)

        homeBgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        homeSetBtn.snp.makeConstraints { make in
            make.right.equalTo(-24.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 25.fitScale())
            make.width.height.equalTo(44.fitScale())
        }
        homeHotTitleImgView.snp.makeConstraints { make in
            make.left.equalTo(50.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 137.fitScale())
            make.width.equalTo(357.fitScale())
            make.height.equalTo(172.fitScale())
        }
        homeHotGoBtn.snp.makeConstraints { make in
            make.left.equalTo(125.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 337.fitScale())
            make.width.equalTo(196.fitScale())
            make.height.equalTo(62.fitScale())
        }
        homeHeadHImgView.snp.makeConstraints { make in
            make.right.equalTo(0.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 83.fitScale())
            make.width.equalTo(330.fitScale())
            make.height.equalTo(330.fitScale())
        }
        homeHeadFImgView.snp.makeConstraints { make in
            make.right.equalTo(-189.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 243.fitScale())
            make.width.equalTo(163.fitScale())
            make.height.equalTo(163.fitScale())
        }
        homeOneChipBtn.snp.makeConstraints { make in
            make.left.equalTo(41.fitScale())
            make.top.equalTo(PzPublicConst.ScreenTopSpaceHeight + 464.fitScale())
            make.height.equalTo(homeOneChipBtn.snp.width).dividedBy(314/420.0)
        }
        homeTwoChipBtn.snp.makeConstraints { make in
            make.left.equalTo(homeOneChipBtn.snp.right).offset(40.fitScale())
            make.right.equalTo(-41.fitScale())
            make.top.equalTo(homeOneChipBtn.snp.top)
            make.width.equalTo(homeOneChipBtn.snp.width)
            make.height.equalTo(homeOneChipBtn.snp.height)
        }
        homeNorChipBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(homeOneChipBtn.snp.bottom).offset(49.fitScale())
            make.width.equalTo(690.fitScale())
            make.height.equalTo(170.fitScale())
        }
        homeCrachCipeBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(homeNorChipBtn.snp.bottom).offset(24.fitScale())
            make.width.equalTo(690.fitScale())
            make.height.equalTo(170.fitScale())
        }
    }
    func makeVipStatus(closure: @escaping ()->Void){
        let vipTime = UserDefaults.standard.value(forKey: "pzDefTime") as? Double ?? 0
        let cureentTime = PzPublicConst.photoNowTime
        if cureentTime >= vipTime{
            let vipCtrl = PzVipCtrl()
            let vipNavCtrl = UINavigationController(rootViewController: vipCtrl)
            vipNavCtrl.isNavigationBarHidden = true;
            vipNavCtrl.modalPresentationStyle = .fullScreen;
            self.present(vipNavCtrl, animated: true) {}
        }else{
            closure()
        }
    }
    @objc func homeSet(){
        let meCtrl = PzMeCtrl()
        self.navigationController?.pushViewController(meCtrl, animated: true)
    }
    @objc func homeHot(){
        makeVipStatus {
            let norPhotoCtrl = PzNorPhotoCtrl()
            self.navigationController?.pushViewController(norPhotoCtrl, animated: true)
        }
    }
    @objc func homeOneChip(){
        makeVipStatus {
            let photoCtrl = PzPhotoCtrl()
            let photoOneMdl = PzPhotoSubModel()
            photoOneMdl.name = "一寸";
            photoOneMdl.enName = "one inch";
            photoOneMdl.bgIndex = "1";
            photoOneMdl.specs1 = "295 x 413px";
            photoOneMdl.specs2 = "25mm x 35mm";
            photoCtrl.photoSubMdl = photoOneMdl
            self.navigationController?.pushViewController(photoCtrl, animated: true)
        }
    }
    @objc func homeTwoChip(){
        makeVipStatus {
            let photoCtrl = PzPhotoCtrl()
            let photoTwoMdl = PzPhotoSubModel()
            photoTwoMdl.name = "二寸";
            photoTwoMdl.enName = "two inches";
            photoTwoMdl.bgIndex = "1";
            photoTwoMdl.specs1 = "413 x 579px";
            photoTwoMdl.specs2 = "35mm x 49mm";
            photoCtrl.photoSubMdl = photoTwoMdl
            self.navigationController?.pushViewController(photoCtrl, animated: true)
        }
    }
    @objc func homeNorChip(){
        makeVipStatus {
            let udid:String = UIDevice.current.identifierForVendor?.uuidString ?? ""
            let url:String = PzPublicConst.photoLiveAllPhotoUrl
            let dataUrl = url.replacingOccurrences(of:"%@", with: udid)
            
           let webCtrl = PzWebCtrl()
            webCtrl.webUrl = dataUrl
            webCtrl.isHiddenBack = true
            self.navigationController?.pushViewController(webCtrl, animated: true)
        }
    }
    @objc func homeCrachCipe(){
        makeVipStatus {
            let coinCtrl = PzCoinCtrl()
            self.navigationController?.pushViewController(coinCtrl, animated: true)
        }
    }
}
